<?php
namespace app\manage\controller;

class SystemRole extends Base
{
    public function index(){
        if(request()->isAjax()){
            $page = input('page');
            $limit = input('limit');
            $id = input('id');
            $roleName = input('role_name');

            $where = [];
            !empty($id) && $where['id'] = $id;
            !empty($roleName) && $where['rolename'] = ['like', '%'.$roleName.'%'];

            if( ! $count=model('SystemRole')->where($where)->count() ){
                $this->layTable();
            }

            $data = model('SystemRole')
                ->where($where)
                ->limit(($page-1)*$limit, $limit)
                ->order('id asc')
                ->select();

            $this->layTable(0, '管理员列表', $data, $count);
        }else{
            $select = model('SystemRole')->field('id, rolename')->select();
            return view('', ['role_id'=>$select]);
        }
    }

    public function add(){
        if( request()->isAjax() ){
            $data = input();
            $addRs = model('SystemRole')->save( $data );
            $addRs ? $this->layJson(0, '添加成功') : $this->layJson(1, model('SystemRole')->getError());
        }else{
            $select = model('SystemPrivilegeNew')->select();
            import('cate.Category');
            $priList = \Category::unlimitedForLayer($select);

            return view('', ['pri_list'=>$priList]);
        }
    }

    public function edit(){
        if( request()->isAjax() ){
            $data = input();
            $editRs = model('SystemRole')->isUpdate(true)->save( $data );
            $editRs!==false ? $this->layJson(0, '编辑成功') : $this->layJson(1, model('SystemRole')->getError());
        }else{
            $find = model('SystemRole')->find( input('id') );
            $select = model('SystemPrivilegeNew')->select();
            import('cate.Category');
            $priList = \Category::unlimitedForLayer($select);
            return view('', ['find'=>$find, 'pri_list'=>$priList]);
        }
    }

    public function del(){
        model('SystemRole')->destroy(input('id')) ? $this->layJson(0, '删除成功') : $this->layJson(1, '删除失败，请重试');
    }
}